{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# LangChain调用本地大模型\n",
    "\n",
    "第1步：下载并安装Ollama\n",
    "\n",
    "第2步：在Ollama下部署大模型：deepseek-r1:7b\n",
    "\n",
    "第3步：在LangChain中调用Ollama下的大模型\n",
    "\n"
   ],
   "id": "306c78397097623c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T09:40:10.639633Z",
     "start_time": "2025-08-27T09:40:08.822400Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "from langchain_ollama import ChatOllama\n",
    "\n",
    "# ChatOpenAI()\n",
    "\n",
    "#此时调用的是本地的大模型。省略base_url、api-key\n",
    "llm = ChatOllama(\n",
    "    model = \"deepseek-r1:7b\"\n",
    ")\n",
    "\n",
    "\n",
    "# llm.invoke(\"你好，请介绍一下你自己！\")\n",
    "\n",
    "\n",
    "messages = [\n",
    "    HumanMessage(content=\"你好，请介绍一下你自己！\")\n",
    "]\n",
    "\n",
    "response = llm.invoke(messages)\n",
    "print(response.content)"
   ],
   "id": "6936c51b06276f65",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<think>\n",
      "您好！我是由中国的深度求索（DeepSeek）公司开发的智能助手DeepSeek-R1。如您有任何任何问题，我会尽我所能为您提供帮助。\n",
      "</think>\n",
      "\n",
      "您好！我是由中国的深度求索（DeepSeek）公司开发的智能助手DeepSeek-R1。如您有任何任何问题，我会尽我所能为您提供帮助。\n"
     ]
    }
   ],
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
